﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using LuaInterface;
using System.IO;
using System.Net;
namespace Urd.Common
{
    public class configuration
    {
        
        private static Lua l=new Lua();        
        public string config_file_path = "server.cfg";
        public string server_bind_ip_adress_or_domain;
        public IPAddress ipaddress;
        public string server_bind_port;
        
        public string shemasfolder=@"shemas\";
        public string tablesfolder=@"tables\";
        public string workersfolder=@"workers\";

        public string database_ip_adress_or_domain;        
        public string database_port;
        public string database_name;
        public string database_username;
        public string database_password;

        public configuration() 
        {
            if (!File.Exists(config_file_path)) 
            {
                throw new FileNotFoundException(config_file_path);
                //File.Create(config_file_path);
            }
            try
            {
                l.DoFile(config_file_path);
            }
            catch (LuaInterface.Exceptions.LuaScriptException e) { debug.Log("[luascript]:" + e.Source + " " + e.Message,debug.loglevel.Errors); }
            catch (LuaInterface.Exceptions.LuaException e) { debug.Log("[lua]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
            catch (Exception e) { debug.Log("[system]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
        }
        public void save()
        {
            string output = "config={\n";
            output += " server={\n";
            output += "bind='" + server_bind_ip_adress_or_domain + "',\n";
            output += "port='" + server_bind_port + "'\n";
            output += "};\n";
            output += "database={\n";
           
            output += "address='" + database_ip_adress_or_domain + "',\n";
            output += "port='" + database_port + "',\n";
            output += "name='" + database_name + "',\n";
            output += "user='" + database_username + "',\n";
            output += "password='" + database_password + "'\n";
            output += "};\n";
            output += "};";
            File.WriteAllText(config_file_path, output);
        }
        public void load() 
        {
            try{
            server_bind_ip_adress_or_domain = l["config.server.bind"] as string;
            IPAddress.TryParse(server_bind_ip_adress_or_domain,out ipaddress);
            server_bind_port = l["config.server.port"] as string;
            
            database_ip_adress_or_domain = l["config.database.address"] as string;
            database_port = l["config.database.port"] as string;
            database_name = l["config.database.name"] as string;
            database_username = l["config.database.user"] as string;
            database_password = l["config.database.password"] as string;

            }
            catch (LuaInterface.Exceptions.LuaScriptException e) { debug.Log("[luascript]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
            catch (LuaInterface.Exceptions.LuaException e) { debug.Log("[lua]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
            catch (Exception e) { debug.Log("[system]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
        }
    }
}
